package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ProjectName: sky-take-out
 * @Titile: DishServiceImpl
 * @Author: 霍静
 * @Description: TODO
 */
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        /**
         * 保存菜品数据
         * 1、创建Dish对象
         * 2、讲dishDTO拷贝到dish对象中
         * 3、将dish对象插入到dish表中（注意：血药插入以后的返回值id）
         * 4、从dishDTO中获取到flavor对象集合
         * 5、并遍历，设置dishId
         * 6、批量插入到dish_flavor中
         */
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        dishDTO.getFlavors().forEach(dishFlavor ->{
            //设置dishId
            dishFlavor.setDishId(dish.getId());

        } );
        //int a = 1/0;//测试事务
        //批量插入
        dishFlavorMapper.insertBatch(dishDTO.getFlavors());

    }

    //分页查询
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO){
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page page  = dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void delete(Long[] ids) {
        /**
         * 1、遍历数组获取id
         * 2、判断如果当前菜品是启售中，不能删除
         * 3、如果菜品被其他泰餐关联，不能删除
         * 4、如果菜品有口味，需要吧口味删除
         */
        for (Long id : ids) {
            Dish dish = dishMapper.findById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                //菜品状态为启售，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            List<SetmealDish> setmealDishes = setmealDishMapper.getByDishId(id);
            if (setmealDishes != null && setmealDishes.size() > 0) {
                //菜品被关联，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            //菜品可以删除
            dishMapper.delete(id);
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .build();
        dishMapper.update(dish);

    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        /**
         * 1、将dishDTO数据拷贝到Dish对象中
         * 2、删除dish_flavor
         * 3、批量插入dish_flavor
         */
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //更新时间
        dish.setUpdateTime(LocalDateTime.now());
        dishMapper.update(dish);
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
        dishFlavorMapper.insertBatch(flavors);

    }

    @Override
    public DishVO getById(Long id) {
        /**
         * 1、根据id查询菜品数据
         * 2、根据菜品id查询口味数据
         * 3、将查询到的数据封装到DishVO中
         */
        Dish dish = dishMapper.findById(id);
        List<DishFlavor> dishFlavorList = dishFlavorMapper.getByDishId(id);
        DishVO dishVO = new DishVO();
        //拷贝属性
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;

    }

    @Override
    public List<Dish> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }
}
